#!/bin/bash
#server password:myzl@root
#mongo  --username admin --password 123456    --authenticationDatabase admin
#netstat -an|grep 9000
#lsof -i:9000
#image path:https://mirrors.aliyun.com/centos/8-stream/isos/x86_64/CentOS-Stream-8-20230830.0-x86_64-dvd1.iso
#firewalld open
echo "---------------------firewalld start----------------------------"
firewall-cmd --zone=public --add-port=80-90/tcp --permanent
firewall-cmd --zone=public --add-port=502-512/tcp --permanent
firewall-cmd --zone=public --add-port=8080-8090/tcp --permanent
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --zone=public --add-port=9443/tcp --permanent
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent
systemctl restart firewalld
echo "---------------------firewalld success--------------------------"
#mkdir init
echo "---------------------mkdir start--------------------------------"
# 工作路径
WORK_PATH="/home/project/"
# 后端工作路径
PLATFORM_WORK_PATH=${WORK_PATH}platform/
# 前端工作路径
UI_WORK_PATH=${WORK_PATH}ui/

if [ ! -d ${WORK_PATH} ]; then
  echo "mkdir "${WORK_PATH}
  mkdir ${WORK_PATH}
else
  echo "exists ${WORK_PATH}"
fi
if [ ! -d ${PLATFORM_WORK_PATH} ]; then
  echo "mkdir "${PLATFORM_WORK_PATH}
  mkdir ${PLATFORM_WORK_PATH}
else
  echo "exists ${PLATFORM_WORK_PATH}"
fi
if [ ! -d ${UI_WORK_PATH} ]; then
  echo "mkdir "${UI_WORK_PATH}
  mkdir ${UI_WORK_PATH}
else
  echo "exists ${UI_WORK_PATH}"
fi

cd ~/environment-init
if [  -d bin ]; then
    find bin/ -type f -exec chmod 777 {} \;
    cp -f bin/*start.sh ~/
    cp -f bin/app-start.sh $PLATFORM_WORK_PATH
    cp -f bin/ui-screen-start.sh $UI_WORK_PATH
else
  echo "Missing directory bin"
fi

echo "---------------------mkdir success------------------------------"
#/bin/bash bin/aliyun-repos.sh
#echo "---------------------repos update success------------------------------"
#yum install
echo "---------------------yum install start--------------------------"
sudo yum -y update
echo "---------------------yum install success------------------------"
#git install
echo "---------------------git install start--------------------------"
if ! command -v git;  then
    yum install -y git
else
  echo "git was installed"
fi
echo "---------------------git install success------------------------"
#git telnet
echo "---------------------telnet install start-----------------------"
if ! command -v telnet;  then
    yum -y install telnet
else
  echo "telnet was installed"
fi
echo "---------------------telnet install success---------------------"
#jdk install
echo "---------------------jdk install start--------------------------"
if ! command -v java;  then
#jdk8
    wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
    tar -zxvf jdk-8u141-linux-x64.tar.gz
    mv jdk1.8.0_141/ /usr/local/jdk8
    rm -rf jdk-8u141-linux-x64.tar.gz
#jdk17
    #wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
    #tar -zxvf jdk-17_linux-x64_bin.tar.gz
    wget https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz
    tar -zxvf jdk-17.0.10_linux-x64_bin.tar.gz
    mv jdk-17.0.10/ /usr/local/jdk17
    echo '
export JAVA_HOME=/usr/local/jdk8
export CLASSPATH=.$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile
    source /etc/profile
else
  echo "java was installed"
fi

echo "---------------------jdk install success------------------------"
#maven install
echo "---------------------maven install start------------------------"
if ! command -v mvn;  then
    wget https://repo.huaweicloud.com/apache/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz
    tar -zxvf apache-maven-3.8.8-bin.tar.gz
    mv apache-maven-3.8.8 /usr/local/maven
    rm -rf apache-maven-3.8.8-bin.tar.gz
    echo '
export M2_HOME=/usr/local/maven
export PATH=$PATH:$M2_HOME/bin' >>/etc/profile
    source /etc/profile

else
  echo "mvn was installed"
fi
echo "---------------------maven install success-----------------------"
#node install
echo "---------------------node install start--------------------------"
if ! command -v node;  then
    wget https://registry.npmmirror.com/-/binary/node/latest-v16.x/node-v16.20.2-linux-x64.tar.gz
    tar -zxvf node-v16.20.2-linux-x64.tar.gz
    rm -rf node-v16.20.2-linux-x64.tar.gz
    mv node-v16.20.2-linux-x64 /usr/local/node
    ln -s /usr/local/node/bin/npm /usr/local/bin/
    ln -s /usr/local/node/bin/node /usr/local/bin/
else
  echo "node was installed"
fi
echo "---------------------node install success------------------------"
#nginx install
echo "---------------------nginx install start--------------------------"
if ((!$(systemctl list-unit-files|grep -c nginx) > 0));then
  wget http://nginx.org/download/nginx-1.24.0.tar.gz
  tar -zxvf nginx-1.24.0.tar.gz
  rm -rf nginx-1.24.0.tar.gz
  cd nginx-1.24.0/
  #yum -y install --allowerasing  glibc-devel
  yum -y install gcc gcc-c++ autoconf automake make
  #--allowerasing --skip-broken
  yum -y install openssl openssl-devel
  yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make zlib zlib-devel libffi-devel -y
  ./configure --prefix=/usr/local/nginx
  make && make install
  echo "[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target"> /etc/systemd/system/nginx.service
  systemctl enable nginx.service
  systemctl start nginx.service
else
  echo "nginx was installed"
fi
echo "---------------------nginx install success------------------------"
#docker-compose install
echo "---------------------docker-compose install start-----------------"
if ((!$(systemctl list-unit-files|grep -c docker) > 0));then
  yum -y install  yum-utils device-mapper-persistent-data lvm2
  yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  yum -y erase podman buildah
  yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  echo '{
    "registry-mirrors": [
      "https://xqbfkrxv.mirror.aliyuncs.com",
      "http://hub-mirror.c.163.com",
      "https://docker.mirrors.ustc.edu.cn",
      "https://registry.docker-cn.com"
    ]
  }' >/etc/docker/daemon.json
  systemctl restart docker
  systemctl enable docker
else
  echo "docker-compose was installed"
fi
echo "---------------------docker-compose install success---------------"
#docker-env install
echo "---------------------docker-env install start---------------------"
if (($(docker ps -a|wc -l) < 2));then
  cd ~/environment-init
  if [  -d docker-compose ]; then
      chmod +x docker-compose/rabbit/init/rabbit.sh
      cp -rf docker-compose /usr/local/docker-compose/
      cd /usr/local/docker-compose/
      docker compose up -d
      sleep 1m
      docker exec rabbitmq-compose /bin/bash /docker-entrypoint-initdb.d/rabbit.sh
  fi
else
  echo "docker-env was installed"
fi
echo "---------------------docker-env install success-------------------"
echo "---------------------python install start-----------------------"
rm -rf /usr/bin/python
if ! command -v python;  then
    cd /usr/local
    wget https://www.python.org/ftp/python/3.7.12/Python-3.7.12.tgz
    tar xvf Python-3.7.12.tgz
    cd Python-3.7.12
    /bin/bash ./configure
    make
    make install
    ln -s /usr/local/bin/python3.7 /usr/local/bin/python
else
  echo "python was installed"
fi
echo "---------------------python install end-----------------------"


echo "---------------------paddleseg install start-----------------------"
#cd ~/environment-init
#/bin/bash bin/paddleseg.sh
echo "---------------------paddleseg install end-----------------------"


#project init
echo "---------------------app-init install start-----------------------"
# 项目服务名
APP_SERVICE=app.service
if ((!$(systemctl list-unit-files|grep -c "$APP_SERVICE") > 0));then
  cd ~/environment-init
  /bin/bash bin/app-init.sh
else
  echo "project was Initialized"
fi

echo "---------------------app-init install success---------------------"


#ui init
echo "---------------------frontEnd-init install start-----------------------"
# 项目服务名
cd ~/environment-init
/bin/bash bin/frontEnd-init.sh

echo "---------------------frontEnd-init install success---------------------"